// Cloak multiple affiliate links with colors
// Date 23:51 12/12/2016
// By Ben a.k.a DreamVB

#include <Windows.h>
#include <iostream>
#include <string>

using namespace std;
using std::cout;
using std::endl;

void ColorFont(WORD color){
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, color);
}

void Caption(){
	ColorFont(31);
	cout << "--- Multiple affiliate links cloaker v2.1 ---\n\n";
}

int main(int argc, char *argv[]){
	string sLink[100];
	string sPath = "";
	int NoLinks = 0;
	int i = 0;

	Caption();
	ColorFont(15);
	//Ask how many links to cloak.
	cout << "How many affiliate links to cloak max 100 : ";

	//Get links.
	cin >> NoLinks;

	if (NoLinks < 0 | NoLinks>100){
		cout << "Invaild number of links\n";
		exit(1);
	}

	cin.ignore();

	//Fetch links from user.
	while (i < NoLinks){
		cout << "Enter Link  " << (i + 1) << " : ";
		//Get link
		std::getline(cin, sLink[i]);
		i++;
	}

	//.htaccess file
	//Ask were the .htaccess file will be saved.
	cout << endl << "Were will this php file be located eg /ads/ : ";
	std::getline(cin, sPath);

	i = 0;
	//Clear the screen
	system("cls");

	Caption();
	ColorFont(15);
	cout << "Below is the code for your index.php file.\n\n";
	ColorFont(30);
	cout << "--- CUT CODE ---\n\n";
	ColorFont(11);
	cout << "<?php\n";
	cout << "  $path = array(\n";

	while (i < NoLinks){
		//Display the link
		cout << "  'affiliatelink" << (i + 1) << "' =>  '" << sLink[i] << "',\n";
		//INC counter
		i++;
	}
	cout << ");\n";

	//Write redirect code.
	cout << "if (array_key_exists($_GET['id'], $path))\n";
	cout << "  header('Location: ' .$path[$_GET['id']]);\n";
	cout << "?>\n\n";
	ColorFont(30);
	cout << "--- END CODE ---\n\n";

	ColorFont(15);
	//Write .htaccess code.
	cout << "Below is the code for your .htaccess file.\n\n";
	ColorFont(30);
	cout << "--- CUT CODE ---\n\n";
	ColorFont(14);
	cout << "Options +FollowSymLinks -MultiViews\n";
	cout << "\nRewriteEngine On\n";
	cout << "RewriteBase " << sPath << "\n\n";
	cout << "RewriteCond %{REQUEST_FILENAME} !-d\n";
	cout << "RewriteCond %{REQUEST_FILENAME} !-f\n";
	cout << "RewriteRule ^([^/]+)$ index.php?id=$1 [L]\n\n";
	ColorFont(30);
	cout << "--- END CODE ---\n\n";
	//Clear up.
	ColorFont(15);
	system("pause");
	return EXIT_SUCCESS;
}